python pattern glob extension searching

40

import os

for root, dirs, files in os.walk("my_dir"):
    for file in files:
        if file.endswith(".txt"):
            print(file)
import os

for file in os.listdir("my_dir"):
    if file.endswith(".txt"):
        print(file)
import glob, os

os.chdir("my_dir")

for file in glob.glob("*.txt"):
    print(file)

Comments

Submit
0 Comments